package com.thinksns.sociax.t4.android.event;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.popupwindow.PopupWindowListDialog;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.component.SmallDialog;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;

import java.util.ArrayList;
import java.util.List;

public class EventApplyActivity extends ThinksnsAbscractActivity implements View.OnClickListener {

    private android.widget.EditText etName;
    private android.widget.TextView tvSex;
    private android.widget.EditText etPhone;
    private android.widget.EditText etBeizhu;
    private android.widget.TextView btnDoApply;
    private SmallDialog dialog;

    private String eid;
    private int sex = -1; // 标志未选择性别
    private String name;
    private String phone;
    private String note;

    public final static int RESULT_APPLY_OK = 101;

    @Override
    public String getTitleCenter() {
        return "报名活动";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_apply);
        initIntentData();
        findViews();
        initListener();
    }

    private void initListener() {
        tvSex.setOnClickListener(this);
        btnDoApply.setOnClickListener(this);
    }

    private void findViews() {
        this.btnDoApply = (TextView) findViewById(R.id.btn_do_apply);
        this.etBeizhu = (EditText) findViewById(R.id.et_apply_beizhu);
        this.etPhone = (EditText) findViewById(R.id.et_apply_phone);
        this.tvSex = (TextView) findViewById(R.id.tv_apply_sex);
        this.etName = (EditText) findViewById(R.id.et_apply_name);
        dialog = new SmallDialog(this, "操作中...");
    }

    private void initIntentData() {
        if (!getIntent().hasExtra("eid")) {
            throw new AssertionError("there need a eid at EventApplyActivity");
        } else {
            eid = getIntent().getStringExtra("eid");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_event_apply;
    }

    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_apply_sex:
                selectSex();
                break;
            case R.id.btn_do_apply:
                checkApply();
                break;
        }
    }

    //check for apply
    private void checkApply() {
        name = etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showToast("姓名不能为空");
            return;
        }
        if (sex == -1) {
            ToastUtils.showToast("请选择性别");
            return;
        }
        phone = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showToast("请输入联系方式");
            return;
        }
        note = etBeizhu.getText().toString().trim();
        doApply();
    }

    //show select sex dialog
    public void selectSex() {
        final PopupWindowListDialog.Builder builder = new PopupWindowListDialog.Builder(this);
        builder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    sex = 1;
                    tvSex.setText("男");
                    tvSex.setTextColor(getResources().getColor(R.color.black));
                } else {
                    sex = 0;
                    tvSex.setText("女");
                    tvSex.setTextColor(getResources().getColor(R.color.black));
                }
            }
        });
        List<String> datas = new ArrayList<String>();
        datas.add("男");
        datas.add("女");
        builder.create(datas);
    }

    //do apply
    private void doApply() {
        dialog.show();
        ((Thinksns) getApplicationContext()).getApiEvent().enrollment(eid, name, sex, 1, phone, note,
                new ApiHttpClient.HttpResponseListener() {
                    @Override
                    public void onSuccess(Object result) {
                        if ((Boolean) result) {
                            dialog.dismiss();
                            setResult(RESULT_APPLY_OK);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Object result) {
                        dialog.dismiss();
                        ToastUtils.showToast((String) result);
                    }
                });
    }

}
